package cn.tedu.ivos.dictoption.controller;

import cn.tedu.ivos.base.response.JsonResult;
import cn.tedu.ivos.dictoption.pojo.dto.DictOptionQuery;
import cn.tedu.ivos.dictoption.pojo.dto.DictOptionSaveParam;
import cn.tedu.ivos.dictoption.pojo.vo.DictOptionVO;
import cn.tedu.ivos.dictoption.service.DictOptionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/v1/dictOption")
public class DictOptionController {
    @Autowired
    DictOptionService dictOptionService;
    @GetMapping("select")
    public JsonResult selectDictOption(DictOptionQuery dictOptionQuery){
        log.debug("查询字典项: dictOptionQuery={}",dictOptionQuery);
        List<DictOptionVO> dictOptionVOS = dictOptionService.selectDictOption(dictOptionQuery);
        return JsonResult.ok(dictOptionVOS);
    }
    @PostMapping("save")
    public JsonResult saveDictOption(DictOptionSaveParam dictOptionSaveParam){
        log.debug("保存字典项: dictOptionSaveParam={}",dictOptionSaveParam);
        dictOptionService.saveDictOption(dictOptionSaveParam);
        return JsonResult.ok();
    }
    @PostMapping("delete/{id}")
    public JsonResult deleteDictOption(@PathVariable Long id){
        log.debug("删除字典项: id={}",id);
        dictOptionService.deleteDictOption(id);
        return JsonResult.ok();
    }

    /**
     * 定义一个字典code查询该字典下所有字典项的方法
     */
    @GetMapping("select/{code}")
    public JsonResult selectDictOptionByCode(@PathVariable String code){
        log.debug("根据字典code查询该字典下所有字典项:code={}",code);
        List<DictOptionVO> dictOptionVOS = dictOptionService.selectDictOptionByCode(code);
        return JsonResult.ok(dictOptionVOS);
    }
}
